package com.aaa.ssm.service.impl;

import com.aaa.ssm.dao.SysUserDao;
import com.aaa.ssm.entity.SysUser;
import com.aaa.ssm.service.SysUserService;
import com.aaa.ssm.util.Page;
import com.aaa.ssm.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ fileName:SysUserService
 * @ description:
 * @ author:张圣福
 * @ createTime:2023/10/8 20:24
 * @ version:1.0.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;
    @Override
    public SysUser checkUser(String userName, String password) {
        SysUser sysUser=new SysUser();
        sysUser.setUserName(userName);
        sysUser.setPassword(password);
        List<SysUser> userList=sysUserDao.queryAll(sysUser);
        if (userList!=null&&userList.size()>0){
            System.out.println("==============="+userList);
            return userList.get(0);
        }

        return null;

    }

    @Override
    public int add(SysUser sysUser) {
        return sysUserDao.add(sysUser);
    }

    @Override
    public PageInfo queryByPage(Page<SysUser> page) {
        //设值分页参数   startPage(int pageNum, int pageSize)  pageNum 第几页  pageSize 页面
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        return new PageInfo(sysUserDao.queryAll(page.getData()));
    }

    @Override
    public List<SysUser> queryAll(SysUser sysUser) {
        return sysUserDao.queryAll(sysUser);
    }

    @Override
    public int update(SysUser sysUser) {
        return sysUserDao.update(sysUser);
    }

    @Override
    public int deleteById(Integer userId) {
        return sysUserDao.deleteById(userId);
    }

    @Override
    public SysUser queryById(Integer userId) {
        return sysUserDao.queryById(userId);
    }


    /*
     * 分配角色
     * */
    @Override
    public Result confirmAllocationRole(Integer userId, Integer[] checkedRole) {
        sysUserDao.deleteByUserId(userId);
        int row = sysUserDao.addUserRole(userId, checkedRole);
        return new Result(200,"分配成功",null);
    }

    /*
     * 删除用户对应的角色
     * */
    @Override
    public int deleteByUserId(Integer userId) {
        return sysUserDao.deleteByUserId(userId);
    }

}
